package de.hdu.pms.ho.dao;

import org.apache.log4j.Logger;


import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.pms.ho.model.Personal;

/**
 * Class for all necessary functions with {@link Personal} content
 * @author Dominic Feig
 */
public class PersonalDao extends HibernateDaoSupport{	//DAO-Klassen benutzen eine Oberklasse
														//Hibernatedaosupport hat eine session-id
	public static Logger Log = Logger.getLogger(PersonalDao.class);
	
	public static void setLog(Logger log){
		Log = log;
	}
	
	private PersonalDao personalDao;
	
	public PersonalDao getPersonalDao() {
		return personalDao;
	}
	public void setPersonalDao(PersonalDao personalDao){
	this.personalDao = personalDao;
}
	

	/**
	 * @param personal
	 * @return {@link Personal} object
	 * This method is save a new object or edit a old one
	 */
	public Personal save(Personal personal) {
		HibernateTemplate template = getHibernateTemplate(); //Instanzenmethode aus der Oberklasse
		template.saveOrUpdate(personal);
		return personal;		
	}

	/**
	 * @return all {@link Personal} objects in one list
	 */
	public List<Personal> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List<Personal> results = template.loadAll(Personal.class);
		return (List<Personal>) results;
	}
	
	
	/**
	 * @param personal
	 *  Delete one {@link Personal} object
	 */
	public void delete(Personal personal){
		HibernateTemplate template = getHibernateTemplate();
		template.delete(personal);
	}
	
	/**
	 * @param id
	 * @return
	 * This method is searching for one specific {@link Personal} object
	 */
	public Personal findById(Integer id){
		HibernateTemplate template = getHibernateTemplate();
		return (Personal) template.get(Personal.class, id);
	}
	
	public void create(String vorname, String nachname) {
		HibernateTemplate template = getHibernateTemplate();
		Personal personal = new Personal(vorname, nachname);
		template.persist(personal);
	}
}
